import 'package:dio/dio.dart';

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: const Text("File Network Widget Demo"),
      ),
      body: BlockDownloadWidget(),
    ),
  ));
}

class BlockDownloadWidget extends StatefulWidget {
  const BlockDownloadWidget({Key? key}) : super(key:key);

  @override
  _BlockDownloadWidgetState createState() => _BlockDownloadWidgetState();
}

class _BlockDownloadWidgetState extends State<BlockDownloadWidget> {

  final Dio _dio = Dio();

  get requestFuture async => await _dio.get("https://api.github.com/orgs/flutterchina/repos");

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      child: FutureBuilder(
          future:  requestFuture,
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            //请求完成
            if (snapshot.connectionState == ConnectionState.done) {
              print(snapshot.data);
              Response response = snapshot.data as Response;

              //发生错误
              if (snapshot.hasError) {
                return Text(snapshot.error.toString());
              }
              //请求成功，通过项目信息构建用于显示项目名称的ListView
              return ListView(
                children: response.data.map<Widget>((e) =>
                    ListTile(title: Text(e["full_name"]))
                ).toList(),
              );
            }
            //请求未完成时弹出loading
            return const CircularProgressIndicator();
          }
      ),
    );
  }


}